package com.betty.message01;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static android.R.id.list;

public class MainActivity extends AppCompatActivity {
    private Uri SMS_INBOX=Uri.parse("content://sms/inbox");
    private ListView lv_sms;
    private ArrayList<HashMap<String,String>> list;
    private SimpleCursorAdapter adapter;
    private Cursor cursor;
    private ContentResolver resolver;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv_sms = (ListView)findViewById(R.id.lv_sms);
       adapter = new SimpleCursorAdapter(this,R.layout.list_item,null,new String[]{"address","body"},new int[]{R.id.tv_number,R.id.tv_body});
        lv_sms.setAdapter(adapter);
        resolver=getContentResolver();
    }

    @Override
    protected void onDestroy() {
        if (cursor!=null)
            cursor.close();
        super.onDestroy();
    }
    public void showSMS(View view){
        cursor = resolver.query(SMS_INBOX,null,null,null,null);
        adapter.swapCursor(cursor);
    }

//    private ArrayList<HashMap<String,String>> readAllSms(){
//        cursor = managedQuery(SMS_INBOX,new String[]{"address","body"},null,null,null);
//        while (cursor.moveToNext()){
////            String add = cursor.getString(cursor.getColumnIndexOrThrow("address"));
////            String name = cursor.getString(cursor.getColumnIndex("body"));
//            adapter.swapCursor(cursor);
//        }
////        HashMap<String,String> item =new HashMap<String, String>();
////        if (cursor.moveToFirst()) {
////            int addressID = cursor.getColumnIndex("address");
////            int personID = cursor.getColumnIndex("person");
////            int bodyID = cursor.getColumnIndex("body");
////            do {
////                String address = cursor.getString(addressID);
////                String person = cursor.getString(personID);
////                String body = cursor.getString(bodyID);
////                item.put("address", address);
////                item.put("person", person);
////                item.put("body", body);
////                list.add(item);
////            } while (cursor.moveToNext());
////
////        }
////        return list;
//    }
}
